package wxshop.web.admin.controller;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import wxshop.api.admin.model.wx.MenuInfo;
import wxshop.api.admin.model.wx.SecondMenu;
import wxshop.api.admin.model.wx.WxMenu;
import wxshop.web.admin.tools.ConstValues;
import wxshop.web.admin.tools.GetToken;
import wxshop.web.admin.tools.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 121874694@qq.com on 2018/3/4.
 */
@Component
@RestController
@RequestMapping("/menu")
public class WxMenuController {
	String urls = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="+ ConstValues.TOKEN;
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public void delete() throws Exception {
		GetToken.getToken();
		HttpUtil.doGet(urls,ConstValues.CHARSET);
	}
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public void add() throws Exception {
		GetToken.getToken();
		//不能直接修改菜单，删除菜单再添加
		HttpUtil.doGet(urls,ConstValues.CHARSET);
		String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+ConstValues.TOKEN;
		//添加二级菜单详细信息
		SecondMenu sm = new SecondMenu();
		sm.setName("服务与建议");
		MenuInfo[]mi = new MenuInfo[]{
				new MenuInfo("view","查订单和物流",null,"http://www.baidu.com"),
				new MenuInfo("view","联系商家客服","dsadasd","http://www.baidu.com"),
				new MenuInfo("view","我要退款退货","dsadasd","http://www.baidu.com"),
				new MenuInfo("view","问题反馈","dsadasd","http://www.baidu.com"),
				new MenuInfo("view","0元入驻","dsadasd","http://www.baidu.com"),

		};
		sm.setSub_button(mi);
//		SecondMenu sm1 = new SecondMenu();
//		sm1.setName("会员中心");
//		MenuInfo[]mi1 = new MenuInfo[]{
//				new MenuInfo("view","登录注册",null,"http://www.baidu.com"),
//				new MenuInfo("view","会员中心","dsadasd","http://www.baidu.com"),
//				new MenuInfo("view","积分补录","dsadasd","http://www.baidu.com"),
//
//		};
//		sm1.setSub_button(mi1);
		//添加一级菜单
		WxMenu wxMenu = new WxMenu();
		String uidUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ConstValues.APP_ID
				+"&redirect_uri=http://lj.tunnel.qydev.com/code"
				+"&response_type=code&scope=snsapi_userinfo#wechat_redirect";
		Object[]oj = new Object[]{
				new MenuInfo("view","❤进入商城","nulldasda",uidUrl),
				new MenuInfo("view","❤元宵福利","nulldasda","http://www.taobao.com"),
				sm

		};
		wxMenu.setButton(oj);
		ObjectMapper om = new ObjectMapper();
		System.out.println(om.writeValueAsString(wxMenu));
		HttpUtil.doPost(url,om.writeValueAsString(wxMenu),ConstValues.CHARSET);
	}
}
